//
//  NutritionViewController.m
//  WE
//
//  Created by iMacOne on 15/11/23.
//  Copyright © 2015年 UNOHACHA. All rights reserved.
//

#import "NutritionViewController.h"
#import "Size.h"
#import "ToolUtil.h"
#import "ScroCell.h"
#import "ThreeGuideCell.h"
#import "UsualQuestionCell.h"
#import "TitleAndImageVCell.h"
#import "NutOpionCell.h"
#import "DisAndNutViewController.h"
#import "UsualDisaViewController.h"
#import "NutritionAndNurseViewController.h"
#import "InfirmaryViewController.h"
#import "UsualDisDetailControl.h"
#import "EMMallSectionView.h"
#import "OnLineAdviControl.h"
#import "ScrollDetailsControl.h"
#import "SearchView.h"
@interface NutritionViewController ()
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, strong)SearchView * searchView;

@end

@implementation NutritionViewController
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [self.searchView endEditing:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"疾病与营养";
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[NutOpionCell class] forCellReuseIdentifier:@"NutOpionCell"];
    [self setExtraCellLineHidden:self.tableView];
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (void)doClickBtn:(NSInteger)number cell:(UITableViewCell *)cell
{
    [self.searchView endEditing:YES];
    if ([cell isKindOfClass:[ThreeGuideCell class]]) {
        if (number == 100) {
            DisAndNutViewController * disVC = [[DisAndNutViewController alloc] init];
            [self.navigationController pushViewController:disVC animated:YES];
            
            NSLog(@"健康评估");
        }else if (number == 101){
            NSLog(@"调理");
            NutritionAndNurseViewController *nutrition = [[NutritionAndNurseViewController alloc] init];
            nutrition.view.backgroundColor = [UIColor whiteColor];
            [self.navigationController pushViewController:nutrition animated:YES];
        }else if (number == 102){
             NSLog(@"咨询");
            [self.navigationController pushViewController:[[OnLineAdviControl alloc] init] animated:YES];
        }
    }else if ([cell isKindOfClass:[ScroCell class]]){
        NSLog(@"轮播");
        [self.navigationController pushViewController:[[ScrollDetailsControl alloc] init] animated:YES];
        
    }else if ([cell isKindOfClass:[UsualQuestionCell class]]){
        if (number == 200) {
            [self.navigationController pushViewController:[[InfirmaryViewController alloc] init] animated:YES];
        }else{
        UsualDisaViewController * usuVC = [[UsualDisaViewController alloc] init];
        [self.navigationController pushViewController:usuVC animated:YES];
        }
    }
}
- (UIView *)creatView
{
    UIView * aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WindowWidth, 50)];
    self.searchView = [[SearchView alloc] initWithParent:aView _point:CGPointMake(0, 0) _placeholder:@"请输入疾病" _callback:^(NSMutableDictionary *dic) {
        NSLog(@"%@",dic[@"keyword"]);
    }];
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(280, 15, 80, 30);
    [btn setTitle:@"更多疾病" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    btn.titleLabel.font = FONT(14);
    [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    [aView addSubview:btn];
    return aView;
}
- (void)clickBtn:(UIButton *)btn
{
    [self.navigationController pushViewController:[[InfirmaryViewController alloc] init] animated:YES];
    
}
- (UITableView *)tableView
{
    if (_tableView == nil) {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0,WindowWidth , WindowHeight ) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.showsVerticalScrollIndicator = NO;
        
    }
    return _tableView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 50;
    }
    return 10;
    
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return [self creatView];
    }
    EMMallSectionView *sectionView =  [EMMallSectionView showWithName:nil];
    sectionView.sign = @"1";
    sectionView.tableView = self.tableView;
    sectionView.section = section;
    return sectionView;
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 4 ) {
        return 5;
    }
        return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        ThreeGuideCell * cell = [[ThreeGuideCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        cell.delegate = self;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else if (indexPath.section == 2){
        ScroCell * cell = [[ScroCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        cell.delegate = self;
        return cell;
    }else if (indexPath.section == 3){
        UsualQuestionCell * cell = [[UsualQuestionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        cell.delegate = self;
        return cell;
    }else if (indexPath.section == 4){
        if (indexPath.row == 0) {
            TitleAndImageVCell * cell = [[TitleAndImageVCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

            return cell;
        }else{
            static NSString * cellIdentifier = @"NutOpionCell";
            NutOpionCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

            return cell;
        }
    }

    static NSString * cellIdentifier = @"cell8";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.backgroundColor = [UIColor whiteColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }
    return cell;
   
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [self.searchView endEditing:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    if (indexPath.section == 4) {
        UsualDisDetailControl * freeVC = [[UsualDisDetailControl alloc] init];
        
        [self.navigationController pushViewController:freeVC animated:YES];
    }    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
        return 5;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 0;
    }else if (indexPath.section == 2){
        return 110;
    }else if (indexPath.section == 3) {
        return 160;
    }else if (indexPath.section == 4){
        if (indexPath.row == 0) {
            return 40;
        }
    }
        return 84;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

-(void)setExtraCellLineHidden: (UITableView *)tableView
{
    UIView *coverView = [UIView new];
    tableView.tableFooterView = coverView;
}

@end
